package cn.wolfcode.ws;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/{token}")
@Component
public class WSServer {
    public static ConcurrentHashMap<String, Session> clients = new ConcurrentHashMap<>();
    @OnOpen
    public void onOpen(Session session, @PathParam( "token") String token){
        System.out.println("客户端连接===>"+token);
        clients.put(token,session);
    }
//    @OnMessage
//    public void onMessage(@PathParam( "token") String token,String msg){
//        System.out.println("收到客户端:"+token+"的消息,内容为:"+msg);
//    }
    @OnClose
    public void onClose(@PathParam( "token") String token){
        clients.remove(token);
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
    }
}